package com.moon.interface_api.job;



import com.alibaba.fastjson.JSONObject;
import com.moon.interface_api.domain.DomainChannelDO;
import com.moon.interface_api.persistence.CheckDao;
import com.moon.interface_api.utils.HttpUtils;
import com.moon.interface_api.utils.WxDomanCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class OrderStatTask {
   
    @Autowired
    private CheckDao checkDao;

    static String WX_URL_CHECK="http://api.jiazi.wang/weixin/wx_domain.php?user=tuoluofo&key=c7bee3e84adb00d6ffa94433f9292bb7";
    private static final SimpleDateFormat hourDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
    private static final SimpleDateFormat dayDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Async
    //@Scheduled(cron = "0 0 */3 * * ?")
    @Scheduled(initialDelay = 1000, fixedDelay = 3*1000*60*60)
    public void  doTask()  {
        System.out.println("**********************定时统计开始"+ hourDateFormat.format(new Date())+"**********************");
        List<DomainChannelDO> rows = checkDao.listChanel();
            for (DomainChannelDO domainChannel : rows) {
                int channel_id = domainChannel.getChannelId();//2
                String	loginDomain=domainChannel.getLoginDomain();
                String checkUrl = "t"+channel_id+"."+loginDomain;//3
                if(loginDomain!=null&&!"".equals(checkUrl)){
                    WxDomanCheck result = wxDomanCheck(checkUrl);
                    int aliResult = aliDomanCheck(checkUrl);
                }

            }

        List<DomainChannelDO> siteRows = checkDao.listSite();
            for (DomainChannelDO domainChannel : siteRows) {
                int channel_id = domainChannel.getChannelId();//2
                String	siteDomain = domainChannel.getLoginDomain();
                String checkUrl = "t"+channel_id+"."+siteDomain;//3
                if(siteDomain!=null&&!"".equals(checkUrl)){
                    WxDomanCheck result = wxDomanCheck(checkUrl);
                    int aliResult = aliDomanCheck(checkUrl);
                }

            }
            
        List<DomainChannelDO> spreadRows = checkDao.listSpread();
            for (DomainChannelDO domainChannel : spreadRows) {
                int channel_id = domainChannel.getChannelId();//2
                String	jumpDomain = domainChannel.getLoginDomain();
                String checkUrl = "t"+channel_id+"."+jumpDomain;//3
                if(jumpDomain!=null&&!"".equals(jumpDomain)){
                    int aliResult = aliDomanCheck(checkUrl);
                    WxDomanCheck result = wxDomanCheck(checkUrl);
                }

            }
          

        List<DomainChannelDO> ssRows = checkDao.listSS();
            for (DomainChannelDO domainChannel : ssRows) {
                int channel_id = domainChannel.getChannelId();//2
                String	spreadSiteDomain = domainChannel.getLoginDomain();
                String checkUrl = "t"+channel_id+"."+spreadSiteDomain;//3
                if(spreadSiteDomain!=null&&!"".equals(spreadSiteDomain)){
                    int aliResult = aliDomanCheck(checkUrl);
                    WxDomanCheck result = wxDomanCheck(checkUrl);

                }
            }
        System.out.println("**********************定时器结束"+ hourDateFormat.format(new Date())+"**********************");
    }

    @SuppressWarnings("unused")
    public WxDomanCheck wxDomanCheck(String domain){
        
        String resultJson = null;
        WxDomanCheck result=null;
        
        try {
            resultJson = HttpUtils.sendGet(WX_URL_CHECK, "domain="+domain);

            System.out.println("验证域名："+domain);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                result = JSONObject.parseObject(resultJson, WxDomanCheck.class);
            } catch (Exception e) {
                e.printStackTrace();
                result= new WxDomanCheck();
                result.setStatus("0");
            }

            if("0".equals(result.getStatus())||"3".equals(result.getStatus())){
                //sucessLog.writeLineFlush("\r\n\r\n微信域名:"+domain+"#"+result.getStatus()+":"+result.getErrmsg());
                result.setStatus("0");
            }else{
                System.out.println("\r\n\r\n微信域名:"+domain+"#"+result.getStatus()+":"+result.getErrmsg());
            }
        }catch(Exception e){
            result = new WxDomanCheck();
            result.setStatus("0");
        }
        if(result==null){
            result = new WxDomanCheck();
            result.setStatus("0");
        }
        return result;
    }

    public int  aliDomanCheck(String domain){
        /*int result = HttpUtils.send( "http://"+domain,"");
        if(result==403){
            System.out.println("\r\n\r\n阿里域名:"+domain+"# resultCode"+result);
        }else{
            //sucessLog.writeLineFlush("\r\n\r\n阿里域名:"+domain+"# resultCode"+result);
        }
        return result;*/
        return 200;
    }

    public void  sendMsg(String oldDomain,String newDomain,String desc){

        //changelLog.writeLineFlush("\r\n\r\n"+desc+"域名:"+oldDomain+"被封; 更新为"+newDomain+";");
		/*ArrayList<HashMap<String, String>> rows=Database.getRows(conn, "SELECT pn from domain_pn group name,pn");
		List<String> list = new ArrayList<>();
		for (HashMap<String, String> row : rows) {
			String	sql="insert into monitor.message (src,msg,pn,create_time) " 
					+"value('domai','域名"+oldDomain+"被封; 更新为"+newDomain+"','"+row.get("pn")+"',now)";
			list.add(sql);
			
		}
		Database.executeBatch("mypc8", list);*/
    }
}
